import 'dart:async';

import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:multi_image_picker2/multi_image_picker2.dart';
import 'package:study_online/global/global.dart';
import 'package:uuid/uuid.dart';

Future insertProblem(int couId, String stuId, String content) async {
  return await Global
      .getInstance()
      .dio
      .post(
    "/comm/addPost",
    queryParameters: {
      "couId": couId,
      "stuId": stuId,
      "content": content,
    },
  );
}

Future getProblemsByObject(int couId, int type) async {
  return await Global
      .getInstance()
      .dio
      .get(
    "/comm/getPost",
    queryParameters: {
      "couId": couId,
      "type": type,
    },
  );
}

Future getProblemById(int posId) async {
  return await Global
      .getInstance()
      .dio
      .get(
    "/comm/getPostById",
    queryParameters: {
      "posId": posId,
    },
  );
}

Future getUserProblem(int couId, String stuId) async {
  return await Global
      .getInstance()
      .dio
      .get(
    "/comm/getPostByStuId",
    queryParameters: {
      "couId": couId,
      "stuId": stuId,
    },
  );
}

Future getComment(int postId) async {
  return await Global
      .getInstance()
      .dio
      .get(
    "/comm/getComment",
    queryParameters: {
      "postId": postId,
    },
  );
}

Future<String> uploadImgTomcat(Asset asset, String name) async {
  ByteData byteData;
  List<int> imageData;
  try {
    byteData = await asset.getByteData();
    imageData = byteData.buffer.asUint8List();
    Dio dio = new Dio();
    var formData = FormData.fromMap({
      'file': await MultipartFile.fromBytes(imageData, filename: name),
    });
    dio.options.headers = {"Content-Type": "multipart/form-data"};
    await dio.post('http://192.168.43.173:8080/FileUpload/FileUploadServlet',
        data: formData);
  } catch (e, stack) {
  } finally {
    return "ok";
  }

}

Future getProblemsByStuId(int couId, String stuId) async {
  return await Global.getInstance().dio.get(
    "/comm/getPostByStuId",
    queryParameters: {
      "couId": couId,
      "stuId": stuId,
    },
  );
}





